# - Config file for the @PACKAGE_UPPER_NAME@ package

set(@PACKAGE_UPPER_NAME@_VERSION "@VERSION@")

# Init variables
set(@PACKAGE_UPPER_NAME@_INCLUDE_DIRS)
set(@PACKAGE_UPPER_NAME@_LIBRARIES)

get_filename_component(QT_ROOT "${CMAKE_CURRENT_LIST_DIR}" PATH)

set(QT_QMAKE_EXECUTABLE ${QT_ROOT}/bin/qmake${CMAKE_EXECUTABLE_SUFFIX})

function(_QT4_QUERY_QMAKE VAR RESULT)
  execute_process(COMMAND "${QT_QMAKE_EXECUTABLE}" -query ${VAR}
    RESULT_VARIABLE return_code
    OUTPUT_VARIABLE output
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)
  if(NOT return_code)
    file(TO_CMAKE_PATH "${output}" output)
    set(${RESULT} ${output} PARENT_SCOPE)
  endif()
endfunction()

set(@PACKAGE_UPPER_NAME@_QMAKE_EXECUTABLE ${QT_QMAKE_EXECUTABLE})

set(QT_EXECUTABLES
    moc uic rcc lupdate lrelease lconvert qcollectiongenerator qmake
)

foreach(QT_EXE ${QT_EXECUTABLES})
    if(NOT TARGET Qt4::${QT_EXE})
        add_executable(Qt4::${QT_EXE} IMPORTED)
        set_property(TARGET Qt4::${QT_EXE} PROPERTY IMPORTED_LOCATION ${QT_ROOT}/bin/${QT_EXE}${CMAKE_EXECUTABLE_SUFFIX})
    endif()
endforeach()


set(STATIC_QT_MODULES
    QtUiTools
)

foreach(QT_MODULE ${STATIC_QT_MODULES})
    set(_MOD_PATH ${QT_ROOT}/lib)
    if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        set(_MOD_PATH "${_MOD_PATH}/lib${QT_MODULE}")
    else()
        set(_MOD_PATH "${_MOD_PATH}/${QT_MODULE}")
    endif()
    if(EXISTS "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}")
        if(NOT TARGET Qt4::${QT_MODULE})
            set(_INCLUDE_DIR)
            add_library(Qt4::${QT_MODULE} STATIC IMPORTED)
            string(REGEX REPLACE "^QT" "" _stemname ${QT_MODULE})
            set_property(TARGET Qt4::${QT_MODULE} PROPERTY
                INTERFACE_COMPILE_DEFINITIONS "QT_${_stemname}_LIB"
            )
            set(_INCLUDE_DIR "${QT_ROOT}/include" "${QT_ROOT}/include/${QT_MODULE}")
            set_property(TARGET Qt4::${QT_MODULE} PROPERTY
                INTERFACE_INCLUDE_DIRECTORIES ${_INCLUDE_DIR}
            )
            # set_property(TARGET Qt4::${QT_MODULE} PROPERTY
                # INTERFACE_LINK_LIBRARIES "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}"
            # )
            set_target_properties(Qt4::${QT_MODULE} PROPERTIES
                IMPORTED_IMPLIB_RELEASE "${QT_ROOT}/lib"
                IMPORTED_IMPLIB_RELWITHDEBINFO "${QT_ROOT}/lib"
                IMPORTED_LOCATION "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}"
                IMPORTED_LOCATION_RELEASE "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}"
                IMPORTED_LOCATION_RELWITHDEBINFO "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}"
            )
        endif()

        string(TOUPPER ${QT_MODULE} QT_MODULE_UPPER_NAME)
        set(QT_${QT_MODULE_UPPER_NAME}_LIBRARY Qt4::${QT_MODULE} )
        set(QT_${QT_MODULE_UPPER_NAME}_LIBRARIES Qt4::${QT_MODULE} )
        set(QT_${QT_MODULE_UPPER_NAME}_INCLUDE_DIRS ${_INCLUDE_DIR})
        set(QT_${QT_MODULE_UPPER_NAME}_FOUND TRUE)
        set(QT_USE_${QT_MODULE_UPPER_NAME} 1)

        # set(QT4_INCLUDE_DIRS ${QT4_INCLUDE_DIRS} ${QT_${QT_MODULE_UPPER_NAME}_INCLUDE_DIRS})
        # set(QT4_LIBRARIES ${QT4_LIBRARIES} ${QT_${QT_MODULE_UPPER_NAME}_LIBRARIES})
    endif()
endforeach()

set(QT_MODULES
    QtCore QtGui Qt3Support QtSvg QtScript QtTest QtHelp QtWebKit
    QtXmlPatterns phonon QtNetwork QtMultimedia QtNsPlugin QtOpenGL
    QtSql QtXml QtDesigner QtDBus QtScriptTools QtDeclarative
)

foreach(QT_MODULE ${QT_MODULES})
    set(_MOD_PATH ${QT_ROOT}/lib/${QT_MODULE})
    if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        set(_MOD_PATH "${_MOD_PATH}.framework")
    else()
        set(_MOD_PATH "${_MOD_PATH}4")
    endif()
    if(EXISTS "${_MOD_PATH}" OR EXISTS "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}")
        if(NOT TARGET Qt4::${QT_MODULE})
            set(_INCLUDE_DIR)
            add_library(Qt4::${QT_MODULE} SHARED IMPORTED)
            string(REGEX REPLACE "^QT" "" _stemname ${QT_MODULE})
            set_property(TARGET Qt4::${QT_MODULE} PROPERTY
                INTERFACE_COMPILE_DEFINITIONS "QT_${_stemname}_LIB"
            )
            if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
                set_property(TARGET Qt4::${QT_MODULE} PROPERTY FRAMEWORK 1)
                set(_INCLUDE_DIR "${_MOD_PATH}" "${_MOD_PATH}/Versions/4/Headers")
                set_property(TARGET Qt4::${QT_MODULE} PROPERTY
                    INTERFACE_INCLUDE_DIRECTORIES ${_INCLUDE_DIR}
                )
                set_target_properties(Qt4::${QT_MODULE} PROPERTIES
                    IMPORTED_LOCATION_RELEASE "${_MOD_PATH}/Versions/4/${QT_MODULE}"
                    IMPORTED_LOCATION_RELWITHDEBINFO "${_MOD_PATH}/Versions/4/${QT_MODULE}"
                    IMPORTED_LOCATION "${_MOD_PATH}/Versions/4/${QT_MODULE}"
                    IMPORTED_SONAME_RELEASE "@rpath/${QT_MODULE}.framework/Versions/4/${QT_MODULE}"
                    IMPORTED_SONAME_RELWITHDEBINFO "@rpath/${QT_MODULE}.framework/Versions/4/${QT_MODULE}"
                )
            else()
                set(_INCLUDE_DIR "${QT_ROOT}/include" "${QT_ROOT}/include/${QT_MODULE}")
                set_property(TARGET Qt4::${QT_MODULE} PROPERTY
                    INTERFACE_INCLUDE_DIRECTORIES ${_INCLUDE_DIR}
                )
                set_target_properties(Qt4::${QT_MODULE} PROPERTIES
                    IMPORTED_IMPLIB_RELEASE "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}"
                    IMPORTED_IMPLIB_RELWITHDEBINFO "${_MOD_PATH}${CMAKE_STATIC_LIBRARY_SUFFIX}"
                    IMPORTED_LOCATION "${QT_ROOT}/bin/${QT_MODULE}4${CMAKE_SHARED_LIBRARY_SUFFIX}"
                    IMPORTED_LOCATION_RELEASE "${QT_ROOT}/bin/${QT_MODULE}4${CMAKE_SHARED_LIBRARY_SUFFIX}"
                    IMPORTED_LOCATION_RELWITHDEBINFO "${QT_ROOT}/bin/${QT_MODULE}4${CMAKE_SHARED_LIBRARY_SUFFIX}"
                )
            endif()
        endif()

        string(TOUPPER ${QT_MODULE} QT_MODULE_UPPER_NAME)
        set(QT_${QT_MODULE_UPPER_NAME}_LIBRARY Qt4::${QT_MODULE} )
        set(QT_${QT_MODULE_UPPER_NAME}_LIBRARIES Qt4::${QT_MODULE} )
        set(QT_${QT_MODULE_UPPER_NAME}_INCLUDE_DIRS ${_INCLUDE_DIR})
        set(QT_${QT_MODULE_UPPER_NAME}_FOUND TRUE)
        set(QT_USE_${QT_MODULE_UPPER_NAME} 1)
    endif()
endforeach()

set(_FOUND TRUE)
if(Qt4_FIND_COMPONENTS)
    foreach( component ${Qt4_FIND_COMPONENTS} )
        string(TOUPPER ${component} QT_MODULE_UPPER_NAME)
        if (QT_${QT_MODULE_UPPER_NAME}_FOUND)
            set(@PACKAGE_UPPER_NAME@_INCLUDE_DIRS ${@PACKAGE_UPPER_NAME@_INCLUDE_DIRS} ${QT_${QT_MODULE_UPPER_NAME}_INCLUDE_DIRS})
            set(@PACKAGE_UPPER_NAME@_LIBRARIES ${@PACKAGE_UPPER_NAME@_LIBRARIES} ${QT_${QT_MODULE_UPPER_NAME}_LIBRARIES})
        else()
            if(NOT Qt4_FIND_QUIETLY)
                message(WARNING "Failed to find Qt4 component \"${component}\"")
            endif()
            set(_FOUND FALSE)
            break()
        endif()
    endforeach()
endif()

include("${CMAKE_ROOT}/Modules/Qt4Macros.cmake")

set(@PACKAGE_UPPER_NAME@_FOUND ${_FOUND})
